// Initial File - Paul Jack

#ifndef __THREADBASE_H__
#define __THREADBASE_H__

// QT Includes
#include <qobject.h>
#include <qthread.h>
#include <qmutex.h>

#include "workerinterface.h"

class ThreadBase: public QThread
{

public:
    ThreadBase(WorkerInterface *worker = 0, const char * name = 0);
    ~ThreadBase();
    inline bool isRunning();
    void setWorkerInterface(WorkerInterface *pWorker);
    void setName(const char * name);

    inline void stop();

    /// This method is intended to only be called by the worker thread while
    /// inside the work method (since we don't need to reacquire the mutex.
    inline void stopFromWorkerThread();
    static void msleep(unsigned long msecs);

    static void usleep(unsigned long usecs);


protected:
    void run();
    virtual bool work();

    /// Are we running?
    bool running;

    /// Mutex for protecting the running member.
    QMutex mMutexRunning;

    WorkerInterface *mWorker;
    QString mName;
};



// NOTE
// Because these are listed as inline they must be included in the
// header file.  This is because during the pre-processer phase
// this code will be expanded just like a macro.  And if it's not
// in here the expansion will fail and you will get a link error.


inline bool ThreadBase::isRunning()
{
    return QThread::isRunning();
}

inline void ThreadBase::stop()
{
    mMutexRunning.lock();
//    if(!isRunning ())
//        start();
    running = false;
    mMutexRunning.unlock();
}


#endif //__THREADBASE_H__
